from PyQt5.QtWidgets import ( QHBoxLayout, QLabel, QPushButton,QSpinBox, QDoubleSpinBox,QGroupBox,QVBoxLayout,QRadioButton)
from components.actiondialog import ActionDialog

class MouseClickDialog(ActionDialog):
    def __init__(self, action=None, parent=None):
        super().__init__(parent)

        self.action = action if action else {"type": "click", "button": "left", "clicks": 1}

        # 按钮选择
        button_group = QGroupBox("选择鼠标按钮")
        button_layout = QVBoxLayout()
        self.left_radio = QRadioButton("左键")
        self.middle_radio = QRadioButton("中键")
        self.right_radio = QRadioButton("右键")

        if self.action["button"] == "left":
            self.left_radio.setChecked(True)
        elif self.action["button"] == "middle":
            self.middle_radio.setChecked(True)
        else:
            self.right_radio.setChecked(True)

        button_layout.addWidget(self.left_radio)
        button_layout.addWidget(self.middle_radio)
        button_layout.addWidget(self.right_radio)
        button_group.setLayout(button_layout)

        # 点击次数
        clicks_layout = QHBoxLayout()
        clicks_layout.addWidget(QLabel("点击次数:"))
        self.clicks_spin = QSpinBox()
        self.clicks_spin.setRange(1, 100)
        self.clicks_spin.setValue(self.action.get("clicks", 1))
        clicks_layout.addWidget(self.clicks_spin)

        # 按钮
        button = QPushButton("确定")
        button.clicked.connect(self.accept)

        self.layout.addWidget(button_group)
        self.layout.addLayout(clicks_layout)
        self.layout.addWidget(button)

    def accept(self):
        button = "left"
        if self.middle_radio.isChecked():
            button = "middle"
        elif self.right_radio.isChecked():
            button = "right"

        self.action = {
            "type": "click",
            "button": button,
            "clicks": self.clicks_spin.value(),
            "desc": f"鼠标 {button} 键点击 {self.clicks_spin.value()} 次"
        }
        super().accept()
